package com.jltx.xq.simplewatch.home;

import android.os.Bundle;
import android.view.View;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.base.presenter.BasePresenter;
import com.jltx.xq.simplewatch.entity.event.NewsFragmentSwitchEvent;
import com.jltx.xq.simplewatch.news.ui.WeChatNewsFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/19.
 */

public class HomeFragment extends BaseSupportFragment{
    private BaseSupportFragment currentFragment;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_act;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView, Bundle savedInstanceState) {
        if(savedInstanceState == null){
            currentFragment = WeChatNewsFragment.newInstance(getString(R.string.wechat_code));
            replaceLoadRootFragment(R.id.fragment_cotainer_frg_home,currentFragment,false);
        }else {
            currentFragment = findChildFragment(WeChatNewsFragment.class);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        registerEventBus(HomeFragment.this);
    }

    @Override
    public void onStop() {
        super.onStop();
        unregisterEventBus(HomeFragment.this);
    }

    /**
     * 接收eventbus发送过来切换新闻界面的事件
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchNewsfragmentEvent(NewsFragmentSwitchEvent event){
        if (event != null){
            switchNewsfragment(event.getNewFragment());
        }
    }

    /**
     * 切换tab
     * @param contentFragment
     */
    public void switchNewsfragment(BaseSupportFragment contentFragment){
        if(contentFragment != null){
            this.currentFragment = contentFragment;
            getFragmentImpl().replaceTransaction(getChildFragmentManager(),R.id.fragment_cotainer_frg_home,contentFragment,false);
        }
    }

}
